import Constant.constant;
import utils.EmptyListException;
import utils.PosIllegalityException;

import java.util.Arrays;

public class MyArrayList implements IList {
    //存储数据
    private int[] elem;
    //记录数组中有效的数据个数
    private int usedSize;

    public MyArrayList() {
        this.elem = new int[constant.DEFAULT_CAPACITY];
    }

    @Override
    public void add(int data) {
        //1.判断顺序表是否满了
        if(isFull()) {
            //2.如果满了 则要进行扩容操作
            grow();
        }
        //3.存储数据
        this.elem[this.usedSize] = data;
        //4.记录usedSize
        this.usedSize++;
    }
    /**
     * 判断顺序表是否满了
     * @return
     */
    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }
    /**
     * 扩容
     */
    private void grow() {
        this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
    }

    @Override
    public void add(int pos, int data) {
        //1.是不是满的
        if(isFull()) {
            grow();
        }

        String msg = constant.ADD_POS_ILLEGALITY+pos;

        checkPosAdd(pos,msg);
        //2.判断是不是放在usedSize的位置
        if(pos == usedSize) {
            elem[pos] = data;
            usedSize++;
            return;
        }
        //3.移动数据
        for (int i = usedSize-1; i >= pos; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;
        usedSize++;
    }

    private void checkPosAdd(int pos,String msg) {
        if(pos < 0 || pos > usedSize) {
            throw new PosIllegalityException(msg);
        }
    }

    @Override
    //判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    // 获取 pos 位置的元素
    public int get(int pos) {
        if(isEmpty()) {
            throw new EmptyListException(constant.EMPTY_LIST);
        }
        String msg = constant.GET_POS_ILLEGALITY+pos;
        checkPos(pos,msg);
        return elem[pos];
    }

    private void checkPos(int pos,String msg){
        if(pos < 0 || pos >= usedSize) {
            throw new PosIllegalityException(msg);
        }
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }

    @Override
    public void set(int pos, int value) {
        if(isEmpty()) {
            throw new EmptyListException(constant.EMPTY_LIST);
        }

        String msg = constant.SET_POS_ILLEGALITY+pos;
        checkPos(pos,msg);
        elem[pos] = value;
    }

    @Override
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        //1.判断为空是否可以，不可以
        if(isEmpty()) {
            throw new EmptyListException(constant.EMPTY_LIST);
        }
        //2.判断未满是否可以 ，可以
        //3.找出toRemove的下标 判断下标是否存在
        int xiabiao=indexOf(toRemove);
        if(xiabiao==-1){
            System.out.println("你要找的关键字在ArrayList中不存在");
        }else{
            for(int i=xiabiao;i<this.usedSize-1;i++){
                elem[i]=elem[i+1];
            }
            //如果是引用类型这里置为null
//            elem[this.usedSize-1]=null;
            this.usedSize--;
        }
    }
    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        for(int i=0;i<this.usedSize;i++){
            //最正规的写法，如果是引用类型，则全部置为空
            this.elem[i]=0;
        }
        this.usedSize=0;

    }

    @Override
    public void display() {

        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] +" ");
        }
        System.out.println();
    }
}
